package com.spring.boot.controller;

import com.spring.boot.default1.mapper.DefaultDemoMapper;
import com.spring.boot.default1.mapper.DefaultDemoMapper2;
import com.spring.boot.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Base64;
import java.util.Collections;
import java.util.Date;
import java.util.Map;

//@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private DefaultDemoMapper demoMapper;

    @Autowired
    private DefaultDemoMapper2 demoMapper2;

    @RequestMapping("/toLogin")
    public @ResponseBody Map toLogin(@RequestParam("username") String userName,
                        @RequestParam("password")String passWord,
                        HttpServletRequest request, HttpServletResponse response
                        ) throws IOException {

        HttpSession session = request.getSession(true);
        session.setAttribute("user",new User(userName,passWord,new Date()));
        return Collections.emptyMap();
    }

    @RequestMapping("/logout")
    public @ResponseBody Map logout(
                                     HttpServletRequest request, HttpServletResponse response
    ) throws IOException {

        HttpSession session = request.getSession(true);
        session.removeAttribute("user");
        return Collections.emptyMap();
    }
}
